package org.example.tialsproject.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.tialsproject.pojo.Emp;
import org.example.tialsproject.pojo.PageBean;
import org.example.tialsproject.pojo.ResponseResult;
import org.example.tialsproject.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/*
 * 员工管理的controller
 * */
@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    /*获取员工列表的方法(支持条件查询、分页查询)*/
    @GetMapping()
    public ResponseResult fetchEmp(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pageSize, String name, Short gender, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        PageBean pageBean = empService.fetchEmp(page, pageSize, name, gender, begin, end);
        return ResponseResult.success(pageBean);
    }

    /*删除员工方法*/
    @DeleteMapping("/{ids}")
    public ResponseResult deleteEmp(@PathVariable("ids") String ids) {
        empService.deleteEmp(ids);
        return ResponseResult.success("删除部门成功");
    }

    /*查询员工的方法*/
    @GetMapping("/{id}")
    public ResponseResult fetchEmpById(@PathVariable("id") int id) {
        Emp emp = empService.fetchEmpById(id);
        return ResponseResult.success(emp);
    }

    /*添加员工的方法*/
    @PostMapping("")
    public ResponseResult addEmp(@RequestBody Emp emp) {
        empService.addEmp(emp);
        return ResponseResult.success("添加员工成功");
    }

    /*编辑员工的方法*/
    @PutMapping("")
    public ResponseResult updateEmp(@RequestBody Emp emp) {
        empService.updateEmp(emp);
        return ResponseResult.success("编辑员工成功");
    }
}
